package calorie.com.calorie.activity;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.TitleView;

import butterknife.BindView;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.PersonalInfomationConstant;
import calorie.com.calorie.response.ResultDTO;
import calorie.com.calorie.response.User;
import calorie.com.calorie.response.UserAssets;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;

/**
 * 修改个人信息
 */
public class ModifyPersonMsgActivity extends MvpActivity<PersonalInfomationConstant.PersonalInfomationPresent> implements PersonalInfomationConstant.IPersonInfo {
    public static final String FROM = "from";
    @BindView(R.id.titleView)
    TitleView titleView;
    @BindView(R.id.et_purse_address)
    EditText etPurseAddress;
    @BindView(R.id.tv_save)
    TextView tvSave;
    String etHintArr[] = {"输入昵称", "输入钱包地址", "输入收货地址"};
    String titleArr[] = {"昵称", "钱包地址", "收货地址"};
    String tvTextArr[] = {"保存", "修改"};
    String data;
    @Override
    public void initData(Bundle savedInstanceState) {
        String nickName = SPUtils.getString(Constant.NICK_NAME, null, context);
        String receiveAddress = SPUtils.getString(Constant.RECEIVE_ADDRESS, null, context);
        String purseAddress = SPUtils.getString(Constant.PURSE_ADDRESS, null, context);
        int from = getIntent().getIntExtra(FROM, -1);
        String titleText = "";
        String etHint = "";
        String saveText = "";
        switch (from) {
            case 1:
                titleText = titleArr[0];
                if (ObjectUtils.isEmpty(nickName)) {
                    saveText = tvTextArr[0];
                    etHint = etHintArr[0];
                } else {
                    etPurseAddress.setText(nickName);
                    saveText = tvTextArr[1];
                }
                break;
            case 2:
                titleText = titleArr[1];
                if (ObjectUtils.isEmpty(purseAddress)) {
                    saveText = tvTextArr[0];
                    etHint = etHintArr[1];
                } else {
                    etPurseAddress.setText(purseAddress);
                    saveText = tvTextArr[1];
                }
                break;
            case 3:
                titleText = titleArr[2];
                if (ObjectUtils.isEmpty(receiveAddress)) {
                    saveText = tvTextArr[0];
                    etHint = etHintArr[2];
                } else {
                    etPurseAddress.setText(receiveAddress);
                    saveText = tvTextArr[1];
                }
                break;
        }
        if (!ObjectUtils.isEmpty(etHint)) {
            etPurseAddress.setHint(etHint);
        }
        tvSave.setText(saveText);
        titleView.setTitleText(titleText);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_person_msg;
    }


    @OnClick(R.id.tv_save)
    public void onViewClicked() {
        int from = getIntent().getIntExtra(FROM, -1);

        String saveText = tvSave.getText().toString();
        if (tvTextArr[0].equals(saveText)) {
             data = etPurseAddress.getText().toString().trim();
            if (data.equals("")) {
                ToastUtils.showLongToast(context, "输入内空不能为空");
                return;
            }
            switch (from) {
                case 1: //保存昵称
                    mPresent.updateNickName(data, StaticValue.user.getId());
                    break;
                case 2://保存钱包地址
                    mPresent.updateMoneyAddress(data, StaticValue.user.getId());
                    break;
                case 3://保存收货地址
                    mPresent.updateReceivingAddress(data, StaticValue.user.getId());
                    break;
            }
        } else {
            etPurseAddress.setText("");
            tvSave.setText(tvTextArr[0]);
            switch (from) {
                case 1: //昵称
                    etPurseAddress.setHint(etHintArr[0]);
                    break;
                case 2://钱包地址
                    etPurseAddress.setHint(etHintArr[1]);
                    break;
                case 3://收货地址
                    etPurseAddress.setHint(etHintArr[2]);
                    break;
            }

        }
    }

    @Override
    public void getUpdateNickNameResult(Response<ResultDTO> response) {
        String code = response.body().getCode();
        if (code.equals("200")) {
            SPUtils.putString(Constant.NICK_NAME,data,context);
            ToastUtils.showLongToast(context, "修改昵称成功");
            Gson gs = new Gson();
            StaticValue.user = gs.fromJson(gs.toJson(response.body().getData()), User.class);
            this.finish();
        } else {
            ToastUtils.showLongToast(context, "服务器错误");
        }

    }

    @Override
    public void getUpdateSexResult(Response<ResultDTO> response) {

    }

    @Override
    public void getUpdateReceivingAddressResult(Response<ResultDTO> response) {
        String code = response.body().getCode();
        if (code.equals("200")) {
            SPUtils.putString(Constant.RECEIVE_ADDRESS,data,context);
            ToastUtils.showLongToast(context, "修改收货地址成功");
            Gson gs = new Gson();
            StaticValue.user = gs.fromJson(gs.toJson(response.body().getData()), User.class);
            this.finish();
        } else {
            ToastUtils.showLongToast(context, "服务器错误");
        }
    }

    @Override
    public void getUpdateMoneyAddressResult(Response<ResultDTO> response) {
        String code = response.body().getCode();
        if (code.equals("200")) {
            SPUtils.putString(Constant.PURSE_ADDRESS,data,context);
            ToastUtils.showLongToast(context, "修改钱包地址成功");
            Gson gs = new Gson();
            StaticValue.userAssets = gs.fromJson(gs.toJson(response.body().getData()), UserAssets.class);
            this.finish();
        } else {
            ToastUtils.showLongToast(context, "服务器错误");
        }
    }

    @Override
    protected PersonalInfomationConstant.PersonalInfomationPresent createPresent() {
        return new PersonalInfomationConstant.PersonalInfomationPresent();
    }
}
